/**
 * @file mainform.cpp
 * @brief MainForm class methods implementation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * @author VestniK (Sergey N.Vidyuk) sir.vestnik@gmail.com
 * @date 24 Jan 2009
 */
#include "mainform.h"

#include <QtGui/QMessageBox>
#include <QtSvg/QSvgRenderer>

#include "sessionclient.h"

#include "connecttohostdialog.h"
#include "startserverdialog.h"
#include "tablewidget.h"

using namespace mjn;

MainForm::MainForm(QWidget *parent): QMainWindow(parent) {
    setupUi(this);
    setCentralWidget(new mjn::TableWidget(this));
    // Add actions to toolbar
    toolBar->addAction(actionStartServer);
    toolBar->addAction(actionConnect);
    toolBar->addSeparator();
    toolBar->addAction(actionQuit);

    mConnectionDialog = new ConnectToHostDialog(this);
    mConnectionDialog->hide();
    mServerDialog = new StartServerDialog(this);
    mServerDialog->hide();
    mLogViewer = new LogViewer(this);
    mLogViewer->hide();

    connect( &mServerThread,SIGNAL(finished()),
             this,SLOT(serverStopped()) );
    connect( &mServerThread,SIGNAL(terminated()),
             this,SLOT(serverStopped()) );
    connect( &mServerThread,SIGNAL(started()),
             this,SLOT(onServerStarted()) );

    connect( &mClient,SIGNAL(connected()),
             this,SLOT(connected()) );
    connect( &mClient,SIGNAL(connectionError(QString)),
             this,SLOT(onConnectionError(const QString &)) );
    connect( &mClient,SIGNAL(connectionFailed()),
             this,SLOT(disconnected()) );
    connect( &mClient,SIGNAL(disconnected()),
             this,SLOT(onClientDisconnected()) );

    connect( mClient.session(),SIGNAL(updateInfo(QList<mjn::SessionInfo>)),
             this,SLOT(onUpdatePlayers(const QList<mjn::SessionInfo> &)) );
}

MainForm::~MainForm() {
}

///////////////////////////////////////////////
// 6 functions responsible for changing of actions state on the toolbar
void MainForm::connecting() {
    actionConnect->setEnabled(false);
    actionStartServer->setEnabled(false);
}

void MainForm::connected() {
    actionConnect->setIcon( QIcon(":/icons/connected.svg") );
    actionConnect->setEnabled(false);
    actionStartServer->setEnabled(false);
}

void MainForm::disconnected() {
    actionConnect->setIcon( QIcon(":/icons/connect.svg") );
    actionConnect->setEnabled(true);
    if ( ! mServerThread.isRunning() ) {
        actionStartServer->setEnabled(true);
    }
}

void MainForm::serverStarting() {
    actionStartServer->setEnabled(false);
    actionConnect->setEnabled(false);
}

void MainForm::serverStarted() {
    actionStartServer->setIcon( QIcon(":/icons/server-running.svg") );
    actionStartServer->setEnabled(false);
    actionConnect->setEnabled(false);
    actionViewLogs->setEnabled(true);
}

void MainForm::serverStopped() {
    actionStartServer->setIcon( QIcon(":/icons/server.svg") );
    actionStartServer->setEnabled(true);
}
///////////////////////////////////////////////

void MainForm::on_actionConnect_activated() {
    if ( mConnectionDialog->exec() != QDialog::Accepted ) {
        return;
    }
    connecting();
    mClient.setName(mConnectionDialog->name());
    mClient.connectToHost(mConnectionDialog->host(),mConnectionDialog->port());
}

void MainForm::on_actionStartServer_activated() {
    if ( mServerDialog->exec() != QDialog::Accepted ) {
        return;
    }
    serverStarting();
    mServerThread.start();
}

void MainForm::on_actionViewLogs_activated()
{
    if (!mLogViewer->isHidden()) {
        mLogViewer->raise();
    } else {
        mLogViewer->show();
    }
}


void MainForm::onServerStarted() {
    serverStarted();
    mClient.setName(mServerDialog->name());
    mClient.connectToHost("localhost",mServerDialog->port());
}

void MainForm::onUpdatePlayers(const QList<SessionInfo> &sessions) {
    playersList->clear();
    foreach (SessionInfo session, sessions) {
        QListWidgetItem *item = new QListWidgetItem(session.name, playersList);
        if (session.disconnected) {
            item->setForeground(QBrush(Qt::gray));
            item->setToolTip(tr("Player is disconnected."));
        }
        playersList->addItem(item);
    }
}

void MainForm::onConnectionError(const QString &msg) {
    QMessageBox::warning(this,tr("Connection error"),msg);
}

void MainForm::onClientDisconnected() {
    disconnected();
    playersList->clear();
}
